package com.harmonyos.cases.view;

import com.harmonyos.cases.listener.CancelListener;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.ui.JBColor;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

@Deprecated
public class CodeDownloadProgressDialog extends DialogWrapper {
    private String descriptionText = "";
    private JLabel description;
    private String progressText = "";
    private JLabel progress;

    private String titleText = "";
    private JLabel title;
    private JButton cancel;
    private String cancelText = "";

    private CancelListener cancelListener;

    public CodeDownloadProgressDialog() {
        super(false);
        init();
    }

    @Override
    protected void init() {
        super.init();
        setUndecorated(true);
        setResizable(false);
    }

    public void setCancelListener(CancelListener cancelListener) {
        this.cancelListener = cancelListener;
    }

    @Override
    protected @Nullable Border createContentPaneBorder() {
        return BorderFactory.createStrokeBorder(new BasicStroke(0.5f));
    }

    public void setDescriptionText(String descriptionText) {
        this.descriptionText = descriptionText;
        if (description != null) {
            description.setText(descriptionText);
            description.repaint();
        }
    }

    public void setProgressText(String progressText) {
        this.progressText = progressText;
        if (progress != null) {
            progress.setText(progressText);
            progress.repaint();
        }
    }

    public void setTitleText(String titleText) {
        this.titleText = titleText;
        if (title != null) {
            title.setText(titleText);
            title.repaint();
        }
    }

    public void setCancelText(String cancelText) {
        this.cancelText = cancelText;
        if (cancel != null) {
            cancel.setText(cancelText);
            cancel.repaint();
        }
    }

    @Override
    protected JComponent createSouthPanel() {
        JPanel panel = new JPanel();
        panel.setPreferredSize(new Dimension(0, 0));
        return panel;
    }

    @Override
    protected @Nullable JComponent createNorthPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setPreferredSize(new Dimension(480, 30));
        panel.setBackground(new JBColor(new Color(74, 78, 82), new Color(74, 78, 82)));
        title = new JLabel();
        title.setHorizontalTextPosition(SwingConstants.CENTER);
        title.setHorizontalAlignment(SwingConstants.CENTER);
        title.setText(titleText);
        panel.add(title, BorderLayout.CENTER);
        return panel;
    }

    @Override
    protected @Nullable JComponent createCenterPanel() {
        JPanel jPanel = new JPanel(new BorderLayout());
        jPanel.setPreferredSize(new Dimension(480, 50));
        jPanel.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10));

        description = new JLabel();
        description.setText(descriptionText);
        jPanel.add(description, BorderLayout.NORTH);

        JProgressBar downloadProgressBar = new JProgressBar();
        downloadProgressBar.setIndeterminate(true);
        downloadProgressBar.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 5));
        jPanel.add(downloadProgressBar, BorderLayout.CENTER);

        progress = new JLabel();
        progress.setText(progressText);
        jPanel.add(progress, BorderLayout.SOUTH);

        cancel = new JButton();
        cancel.setText(cancelText);
        cancel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                doCancelAction();
                if (cancelListener != null) {
                    cancelListener.onCancel();
                }
            }
        });
        jPanel.add(cancel, BorderLayout.EAST);

        return jPanel;
    }

    @Override
    public void show() {
        super.show();

    }

    public void show(Task.Backgroundable downloadBackgroundTask) {
        ProgressManager.getInstance().run(downloadBackgroundTask);
        super.show();
    }

    public void hide() {
        close(DialogWrapper.CLOSE_EXIT_CODE);
    }
}
